package com.example.sm.mycook;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.sm.mycook.fragment.CartFragment;
import com.example.sm.mycook.fragment.HomeFragment;
import com.example.sm.mycook.fragment.MyFragment;
import com.example.sm.mycook.fragment.SaleFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rg;
    private RadioButton home,sale,cart,my;
    private LinearLayout container;
    private HomeFragment homeFragment;
    private SaleFragment saleFragment;
    private CartFragment cartFragment;
    private MyFragment myFragment;
    private List<Fragment> list;
    private int index=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        rg.setOnCheckedChangeListener(this);
        initData();
    }

    private void initData() {
        homeFragment=new HomeFragment();
        saleFragment=new SaleFragment();
        cartFragment=new CartFragment();
        myFragment=new MyFragment();
        list=new ArrayList<>();
        list.add(homeFragment);
        list.add(saleFragment);
        list.add(cartFragment);
        list.add(myFragment);
        getSupportFragmentManager().beginTransaction().add(R.id.container,list.get(0)).commit();


    }

    private void initView() {
        rg = ((RadioGroup) findViewById(R.id.rg));
        home = ((RadioButton) findViewById(R.id.rb_home));
        sale= (RadioButton) findViewById(R.id.rb_sale);
        cart= (RadioButton) findViewById(R.id.rb_cart);
        my= (RadioButton) findViewById(R.id.rb_my);
        container = ((LinearLayout) findViewById(R.id.container));
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId)
        {
            case R.id.rb_home:
                switchFragment(0);
                break;
            case R.id.rb_sale:

                switchFragment(1);

                break;

            case R.id.rb_cart:

                switchFragment(2);

                break;

            case R.id.rb_my:

                switchFragment(3);

                break;

        }
    }

    private void switchFragment(int oIndex)
    {
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();

        Fragment currentFragment=list.get(index);
        Fragment oFragment=list.get(oIndex);

        if (!oFragment.isAdded())
        {
            transaction.add(R.id.container,oFragment).hide(currentFragment).commit();

        }else {
            transaction.show(oFragment).hide(currentFragment).commit();
        }
        index=oIndex;

    }


}
